package com.situ.mall.admin.controller;


import com.situ.mall.admin.entity.User;
import com.situ.mall.admin.service.IUserService;
import com.situ.mall.common.util.StringUtil;
import com.situ.mall.common.vo.ResultVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 闫佳宁
 * @since 2023-09-11
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @Value("${file.path}")
    private String filePath;

    @GetMapping("/page")
    public String page(){
        return "user";
    }

    @PostMapping("/save")
    @ResponseBody
    public void save(User user){
        user.setAvatar(user.getAvatar().replace(filePath,""));
        if (user.getId() == null) {
            user.setCreateTime(LocalDateTime.now());
            user.setPassword(StringUtil.md5(user.getPassword()));
        }
        userService.saveOrUpdate(user);
    }

    @GetMapping("/info")
    @ResponseBody
    public User info(Integer id){
        return userService.info(id);
    }

    @PostMapping("/delete")
    @ResponseBody
    public void delete(Integer id){
        userService.delete(id);
    }


    @GetMapping("/select")
    @ResponseBody
    public TableVO select(Integer page, Integer limit, String username, String name, Integer status){
        return  userService.select(page,limit,username,name,status);
    }

    //批量删除
    @PostMapping("/del")
    @ResponseBody
    public void del(String ids){
        userService.del(ids);
    }

    @PostMapping("/UpdatePassword")
    @ResponseBody
    public TableVO UpdatePassword(String oldPassword,String newPassword,String newPasswordAgain,HttpServletRequest request,HttpServletResponse response){
        return userService.UpdatePassword(oldPassword,newPassword,newPasswordAgain,request,response);
    }

}
